package chess;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: AjaxFetchMsg
 *
 */
 public class AjaxFetchMsg extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public AjaxFetchMsg() {
		super();
	}   	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pid = request.getParameter("pid");
		GameHall gHall = (GameHall)(ContextHelper.getInstance().get("GameHall"));
		Player player = gHall.getPlayerById(pid);
		IMSG msg = null;
		MSGHolder holder = new MSGHolder("MSGS");
		while((msg =player.getMSG())!=null){
			holder.addMsg(msg);
		}
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control","no-cache");
		if(holder.size()!=0){
			//System.out.println(holder.toXML());
			out.println(holder.toXML());
		}else{
			out.println("");
		}
		out.flush();
	}   	  	    
}